//
// Copyright (c) 2009-2011, chunquedong
//
// This file is part of ChunMap project
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
//
// History:
//   2011-05-05  Jed Young  Creation
//

using fwt
using gfx

**
** Tool
**
@Js
class PanTool : Tool
{
  private Int x := 0
  private Int y := 0
  private Bool draging := false

  Bool mustRightButton := false

  override Void actionEvent(Event e)
  {
    if (mustRightButton && e.button != 3 && e.id != EventId.mouseMove) return

    switch (e.id)
    {
      case EventId.mouseDown: mousePressed(e)
      case EventId.mouseUp: mouseReleased(e)
      case EventId.mouseMove: mouseDragged(e)
      default: return
    }
  }


  private Void mouseDragged(Event e)
  {
    if (draging)
    {
      dx := e.pos.x - x
      dy := e.pos.y - y

      map.paintOnBuffer |g|
      {
        map.drawOriginImage(g, dx, dy)
      }

      e.consume
      map.repaint
    }
  }

  private Void mousePressed(Event e)
  {
    draging = true
    x = e.pos.x
    y = e.pos.y
  }

  private Void mouseReleased(Event e)
  {
    if (draging)
    {
      view := map.view

      dx := e.pos.x - x
      dy := e.pos.y - y

      w := view.width / 2f - dx
      h := view.height / 2f - dy

      xx := view.x2World(w)
      yy := view.y2World(h)

      cmd := MoveToCommand(map, xx, yy)
      map.executeCommand(cmd)

      draging = false

      map.renderMap
    }
    x = 0
    y = 0
  }
}

